


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


typedef OnPageResult = void Function(dynamic value);

class AppNavigator {

  static void push(BuildContext context, String name,
      {bool replace = false,
        bool clearStack = false,
        Map? arguments,
        OnPageResult? onPageResult}) {
    unfocus();
    popProgressIfExists(context);
    if (clearStack) {
      Navigator.of(context)
          .pushNamedAndRemoveUntil(name, (route) => false, arguments: arguments)
          .then((value) => {if (onPageResult != null) onPageResult(value)});
    } else if (replace) {
      Navigator.of(context)
          .pushReplacementNamed(name, arguments: arguments)
          .then((value) => {if (onPageResult != null) onPageResult(value)});
    } else {
      Navigator.of(context).pushNamed(name, arguments: arguments).then(
              (value) =>
          {print('sdfsaf'), if (onPageResult != null) onPageResult(value)});
    }
  }

  static void pop(BuildContext context, [dynamic result]) {
    unfocus();
    popProgressIfExists(context);
    Navigator.pop(context, result);
  }

  static void unfocus() {
    //关闭软键盘
    FocusManager.instance.primaryFocus?.unfocus();
  }

  static void popProgressIfExists(BuildContext context) {
    Navigator.popUntil(context, (route) {
      if (route is DialogRoute) {
        RouteSettings settings = route.settings;
        if (settings.name != null && settings.name!.contains('progress')) {
          return false;
        }
      }
      return true;
    });
  }
}